<?php


use Laravel\Redirect;

class Comments_Index_Controller extends Base_Controller
{

    public $restful = true;


    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        //$this->theme->set_theme("default") ;
    }


    public function get_index($post_id = 0, $limit = 10)
    {
			
    	$data['mode'] = "INDEX PAGE";
        $data['ratting'] = Config::get('Comments::config.ratting');
            //--Show Comments--//
       	$data['comments'] = Comments::where('status', '=', 51)->take($limit)->order_by('id', 'DESC')->get();
        return View::make('Comments::comments', $data);
    }

    public function get_review_tab($postID){
    	
    	$pID = $postID;
    	$data['pID'] = $pID;
    	$data['mode'] = "LISTING PAGE TAB";
    	
    	$data['comments'] = Comments::where('post_id','=',$pID)->get();
    	
    	$data['ratting'] = Config::get('Comments::config.ratting');
    	return View::make('Comments::comments',$data);
    	
    }
    public function get_review($postID){
    	 
    	$pID = $postID;
    	$data['pID'] = $pID;
    	$data['mode'] = "LISTING PAGE VIEW";
    	 
    	$data['comments'] = Comments::where('post_id','=',$pID)->get();
    	 
    	$data['ratting'] = Config::get('Comments::config.ratting');
    	return View::make('Comments::comments',$data);
    	 
    }
    
    public function post_comment()
    {
    		
    		
    		
    	//check if its our form
    
    	$rules = array(
    			'rev_name' => 'required|min:4',
    			'rev_email' => 'required|email',
    			'rev_contact' => 'required',
    			'rev_ratting' => 'required',
    			'rev_comment' => 'required|min:25'
    	);
    
    
    
    	$v = Validator::make(Input::all(), $rules);
    
    	if ($v->fails()) {
    
    
    
    		$response = array(
    				'status' => 'Fail',
    				'message' => '<span class="text text-danger">Invalid Input..</span>',
    		);
    	} else {
    
    
    		$data = array(
    				'post_id' => Input::get('post_id'),
    				'name' => trim(Input::get('rev_name')),
    				'email' => trim(Input::get('rev_email')),
    				'contact' => trim(Input::get('rev_contact')),
    				'ratting' => Input::get('rev_ratting'),
    				'comment' => Input::get('rev_comment'),
    				'location' => trim(Input::get('review_location')),
    				'created_at' => Commoncall::current_timestamp(),
    				'updated_at' => Commoncall::current_timestamp(),
    				'ip_address' => $_SERVER["REMOTE_ADDR"],
    		);
    
    		//dd($data);
    
    		if (DB::table('comments')->insert($data)) {
    
    			//--Send Mail to Administrator--//
    
    
    			//--//
    
    			//--Send Mail to Comment Poster --//
    			$parameter = array(
    					'to' => trim(Input::get('rev_email')),
    					'sender_name' => Settings::get('en','site_title').' Team',
    					'reply_to' => 'noreply@gmail.com',
    					'subject' => 'Comment Post',
    					'body' => 'Thank you, You have successfully posted comment on {Business Name}.'
    			);
    
    			//    Commoncall::mail($parameter,null,null);
    
    
    
    
    
    			$response = array(
    					'status' => 'Success',
    					'message' => '<span class="text text-info">Thanks! you posted comments..waiting for moderation.</span>',
    			);
    
    			//  return Redirect::back();
    			//--send mail to Business Owner --//
    
    
    
    
    
    		} else {
    			$response = array(
    					'status' => 'Fail',
    					'message' => '<span class="text text-danger">Ah! something went wrong..</span>',
    			);
    		}
    
    
    
    	}
    
    	return Response::json($response);
    		
    }
    public function post_create()
    {
    	
    	
    //	dd("CREAT");
        //check if its our form
        
        $rules = array(
            'name' => 'required|min:4',
            'email' => 'required|email',
            'contact' => 'required',
            'ratting' => 'required',
            'comment' => 'required|min:25'
        );

        
        $v = Validator::make(Input::all(), $rules);

        if ($v->fails()) {
        	//https://m
        	$response = array(
                'status' => 'Fail',
                'message' => '<div class="alert alert-danger">Invalid Input..</div>',
            );
        } else {
        	
        	
            $data = array(
                'post_id' => Input::get('post_id'),
                'name' => trim(Input::get('name')),
                'email' => trim(Input::get('email')),
                'contact' => trim(Input::get('contact')),
            	'location' => Input::get('location'),	
                'ratting' => Input::get('ratting'),
                'comment' => Input::get('comment'),
                'created_at' => Commoncall::current_timestamp(),
                'updated_at' => Commoncall::current_timestamp(),
                'ip_address' => $_SERVER["REMOTE_ADDR"],
            );
            
         

            if (DB::table('comments')->insert($data)) {

                //--Send Mail to Comment Poster --//
                $parameter = array(
                    'to' => trim(Input::get('email')),
                    'sender_name' => Settings::get('en','site_title').' Team',
                    'reply_to' => 'noreply@gmail.com',
                    'subject' => 'Comment Post',
                    'body' => 'Thank you, You have successfully posted comment on {Business Name}.'
                );

            //    Commoncall::mail($parameter,null,null);





                $response = array(
                    'status' => 'Success',
                    'message' => '<div class="alert alert-info">Thanks! you posted comments..waiting for moderation.</div>',
                );

              //  return Redirect::back();
                //--send mail to Business Owner --//





            } else {
                $response = array(
                    'status' => 'Fail',
                    'message' => '<div class="alert alert-danger">Ah! something went wrong..</div>',
                );
            }
        }

       
        
        return Response::json($response);
        
        
    }

    public function get_all(){
    	
    	$this->theme->set_theme("default") ;
    	$this->theme->set_layout('column1');
    	
    	$data['comments']['all'] = Comments::get();
    	
    	/*-- META DATA */
    	$this->theme->title('All Comments'.' - '.Settings::get('en','site_title'));
    	
    	/*-- BREADCRUMB */
    	
    	Breadcrumb::addbreadcrumb('Home', URL::base());
    	Breadcrumb::addbreadcrumb('All Comments');
    	Breadcrumb::setSeparator('&raquo;');
    	
    	$data['breadcrumb'] = Breadcrumb::generate();
    	
    	return $this->theme->render('Comments::comments',$data);
    	
    }

    
    
}

